// compile:
// run:
#include <chrono>
#include <future>
#include <iostream>
using namespace std;

int func1(int i) {
  cout << "func1" << endl;
  std::this_thread::sleep_for(std::chrono::seconds(2));
  return i;
}
void Test1() {
  std::future<int> fut1 = std::async(std::launch::async, func1, 3);
  int res1 = fut1.get();
}
void Test2() {
  std::packaged_task<int(int)> pt(func1);
  std::future<int> fut2 = pt.get_future();
  // std::thread th1(pt, 4);编译出错
  std::thread th1(std::move(pt), 4);
  th1.detach();
  cout << fut2.get() << endl;
}
void func3(std::promise<int>& pro) {
  std::this_thread::sleep_for(std::chrono::seconds(2));
  pro.set_value(3);
}
void Test3() {
  std::promise<int> pro;
  std::future<int> fut3 = pro.get_future();
  std::thread th1(func3, std::ref(pro));
  // std::thread th1(func3, pro);编译错误
  th1.detach();
  cout << fut3.get() << endl;
}

int main() {
  // Test1();
  Test2();
  // Test3();
  return 0;
}
